package com.gxa.ssm340.service.impl;


import com.gxa.ssm340.common.ErrorCode;
import com.gxa.ssm340.entity.dto.CouponPageDTO;
import com.gxa.ssm340.entity.dto.ManagerLoginDTO;
import com.gxa.ssm340.entity.dto.PageDTO;
import com.gxa.ssm340.entity.dto.UserMenuDTO;
import com.gxa.ssm340.entity.pojo.Get;
import com.gxa.ssm340.entity.pojo.Manager;
import com.gxa.ssm340.entity.pojo.Menu;
import com.gxa.ssm340.entity.vo.UserCouponVO;
import com.gxa.ssm340.exception.BizException;
import com.gxa.ssm340.mapper.CpnCancelMapper;
import com.gxa.ssm340.mapper.MenuMapper;
import com.gxa.ssm340.service.CpnCancelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class CpnCancelServiceImpl implements CpnCancelService {

    @Autowired
    private CpnCancelMapper cpnCancelMapper;

    @Autowired
    MenuMapper menuMapper;

    @Override
    public Manager login(ManagerLoginDTO dto) throws BizException {
        if (StringUtils.isEmpty(dto.getPhone())) {
            throw new BizException(ErrorCode.NULL_FIELD);
        }

        if (StringUtils.isEmpty(dto.getPwd())) {
            throw new BizException(ErrorCode.NULL_FIELD);
        }

        Manager manager = cpnCancelMapper.selectByPhoneAndPwd(dto);

        if (manager == null) {
            throw new BizException(ErrorCode.FAILD_LOGIN);
        }

        List<UserMenuDTO> userMenuDTOS = menuMapper.selectAllMenusByPhone(manager.getPhone());
        if (userMenuDTOS == null){
            throw new BizException(ErrorCode.LACK_URL);
        }


        return manager;
    }

    @Override
    public UserCouponVO[] selectAllByPhone(String phone) {
        return cpnCancelMapper.selectAllByPhone(phone);
    }

    @Override
    public Integer deleteById(Get get) {
        return cpnCancelMapper.deleteById(get);
    }



}
